//
//  STProDetailView.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/12.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STProDetailView.h"

@implementation STProDetailView


-(void)setProDetailViewWithDicData:(NSDictionary *)dicPro{
    hairModel = dicPro;
    [self loadImageScrollView];
    [self loadProIntroduceView];
//    [self loadScrollPageView];
}

-(void)loadImageScrollView{
    imgScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    imgScrollView.showsHorizontalScrollIndicator = NO;
    imgScrollView.showsVerticalScrollIndicator = NO;
    imgScrollView.pagingEnabled = YES;
    imgScrollView.clipsToBounds = NO;
    imgScrollView.bounces = NO;
    
    [imgScrollView setDelegate:self];
    if ([hairModel isKindOfClass:[STHairProDetailInfoModel class]]) {
        STHairProDetailInfoModel * model = hairModel;
        imageViewArray = model.pictrue;
    }
    for(int i=0;i<imageViewArray.count;i++){
        
        
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, imgScrollView.size.height)];
        imageview.contentMode = UIViewContentModeScaleAspectFill;
        imageview.tag = 1000+i;
        [imageview sd_setImageWithURL:[NSURL URLWithString:imageViewArray[i]] placeholderImage:[UIImage imageNamed:@"m-add"]];
        [imgScrollView addSubview:imageview];
        
        imageview.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(actionSelThisImg:)];
        [imageview addGestureRecognizer:tap];
        
//        UIButton * imgBtn = [[UIButton alloc]initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, imgScrollView.size.height)];
//        imgBtn.backgroundColor = [UIColor whiteColor];
//        [imgBtn setImageWithURL:[NSURL URLWithString:imageViewArray[i]] forState:UIControlStateNormal placeholder:[UIImage imageNamed:@"m-add"]];
//        imgBtn.contentMode = UIViewContentModeScaleToFill;
//        imgBtn.tag = 1000+i;
//        [imgBtn addTarget:self action:@selector(actionSelThisImg:) forControlEvents:UIControlEventTouchUpInside];
//        [imgScrollView addSubview:imgBtn];
    }
    [imgScrollView setContentSize:CGSizeMake(imageViewArray.count*SCREEN_WIDTH,imgScrollView.size.height)];
    [self addSubview:imgScrollView];
    
}

-(void)loadProIntroduceView{
    introduceView= [[STHairProIntroduceView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-200, SCREEN_WIDTH, 200)];
    [introduceView setproIntruduceWithDic:hairModel];
   
    [self addSubview:introduceView];
}

-(void)loadScrollPageView{
    pageLab = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame)-20, SCREEN_WIDTH, 20)];
    pageLab.backgroundColor = [UIColor blackColor];
    pageLab.textColor = [UIColor whiteColor];
    pageLab.textAlignment = NSTextAlignmentCenter;
    pageLab.font = FONT(13);
    pageLab.text = [NSString stringWithFormat:@"1/%d",(int)imageViewArray.count];
    [self addSubview:pageLab];
}

-(void)actionSelThisImg:(UITapGestureRecognizer *)sender{
    NSInteger tag = sender.view.tag-1000;
    if([self.delegate respondsToSelector:@selector(proDetailViewImgSelNum:)]){
        [self.delegate proDetailViewImgSelNum:tag];
    }
    
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint offset =  scrollView.contentOffset;
    int currentPage = 0;
    if (offset.x == 0) {
        currentPage = 0;
    } else {
        currentPage = offset.x / CGRectGetWidth(scrollView.frame);
    }
    currentPage ++;
    introduceView.pageLab.text = [NSString stringWithFormat:@"%d/%d",currentPage,(int)imageViewArray.count];
}

@end
